package towork;

import towork.ListNode;

public class testwork2 {
    //力扣 142.环型链表2
    public ListNode detectCycle(ListNode head) {
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(slow==fast){
                break;
            }

        }
        if(fast==null||fast.next==null){
            return null;//环没有入口点
        }
        fast=head;
        while(fast!=slow){//初始fast    fast到入口点的距离等于在相遇点的solw到入口点的距离
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
}
